/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Br.Com.Cds.Academia.FormaPagamento;

import Br.Com.Cds.Academia.ContasPagar.ContasPagarBean;
import Br.Com.Cds.Academia.DAO.AcessoMySQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Pedro
 */
public class FormaPagamentoControl {
    
    PreparedStatement pstm;
    ResultSet rs;
    
    
    String consultaformapagamento = "SELECT * FROM FORMA WHERE FORMA LIKE ?";
    String cadastraformapagamento = "INSERT INTO FORMA (FORMA,ADMINISTRADORA,TIPO,BANCO,AGENCIA,CONTA,PARCELAS,TAXAADM,STATUS)VALUES (?,?,?,?,?,?,?,?,?)";
    String alterarformapagamento = "UPDATE FORMA SET FORMA=?,ADMINISTRADORA=?,TIPO=?, BANCO=?, AGENCIA=?, CONTA=?, PARCELAS=?,TAXAADM=?,STATUS = ? WHERE IDFORMA=?";
    String excluirformapagamento = "DELETE FROM FORMA WHERE IDFORMA=?"; 
    
    
    
    public void cadastraFormaPagamento (FormaPagamentoBean formapagamento){
        try {
            AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement) mysql.conectar().prepareStatement(cadastraformapagamento);
            pstm.setString(1,formapagamento.getForma());
            pstm.setString(2, formapagamento.getAdministradora());
            pstm.setString(3, formapagamento.getTipo());
            pstm.setString(4, formapagamento.getBanco());
            pstm.setString(5, formapagamento.getAgencia());
            pstm.setString(6, formapagamento.getConta());
            pstm.setString(7, formapagamento.getParcelas());
            pstm.setString(8, formapagamento.getTaxaadm());
            pstm.setString(9, formapagamento.getStatus());
            pstm.executeUpdate();
            mysql.desconectar();
    
        } catch (SQLException e) {
            e.printStackTrace ();
            JOptionPane.showMessageDialog(null, "SQL EXCEPTION ! ERRO AO TENTAR CADASTRAR UMA FORMA DE PAGAMENTO!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE); 
        }
}


public void alterarFormaPagamento (FormaPagamentoBean formapagamento){
    
    try {
        AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement) mysql.conectar().prepareStatement(alterarformapagamento);
            pstm.setString(1,formapagamento.getForma());
            pstm.setString(2, formapagamento.getAdministradora());
            pstm.setString(3, formapagamento.getTipo());
            pstm.setString(4, formapagamento.getBanco());
            pstm.setString(5, formapagamento.getAgencia());
            pstm.setString(6, formapagamento.getConta());
            pstm.setString(7, formapagamento.getParcelas());
            pstm.setString(8, formapagamento.getTaxaadm());
            pstm.setString(9, formapagamento.getStatus());
            pstm.setInt(10, formapagamento.getIdforma());
            pstm.executeUpdate();
            mysql.desconectar(); 
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,"SQL EXCEPTION ! ERRO AO TENTAR CADASTRAR UMA FORMA DE PAGAMENTO!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
    }
}

    
  public List <FormaPagamentoBean> listarFormaPagamento (String nomeformapagamento){
         List <FormaPagamentoBean> formapagamento = new ArrayList ();
         
         try {
             AcessoMySQL mysql = new AcessoMySQL();
             pstm = (PreparedStatement) mysql.conectar().prepareStatement(consultaformapagamento);
             pstm.setString(1, nomeformapagamento);
             rs = pstm.executeQuery();
             FormaPagamentoBean alu;
             
             while (rs.next()){
                alu = new FormaPagamentoBean ();
                alu.setIdforma(rs.getInt("IDFORMA"));  
                alu.setTipo(rs.getString("TIPO"));
                alu.setStatus(rs.getString("STATUS"));
                alu.setForma(rs.getString("FORMA"));  
                alu.setAdministradora(rs.getString("ADMINISTRADORA")); 
                alu.setBanco(rs.getString("BANCO")); 
                alu.setAgencia(rs.getString("AGENCIA"));  
                alu.setConta(rs.getString("CONTA"));   
                alu.setParcelas(rs.getString("PARCELAS"));   
                alu.setTaxaadm(rs.getString("TAXAADM"));   
                formapagamento.add(alu);  
             }    
      } catch (Exception e) {
        e.printStackTrace();
         JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR REALIZAR UMA CONSULTA","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
        
      }
        return formapagamento;
      
  }
    
    
  public void excluirFormaPagamento (int idforma){
      
      try {
          AcessoMySQL mysql = new AcessoMySQL();
          pstm = (PreparedStatement) mysql.conectar().prepareStatement(excluirformapagamento);
          pstm.setInt(1,idforma);
          pstm.executeUpdate();
          mysql.desconectar();
            
      } catch (Exception e) {
          e.printStackTrace();
          JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR EXCLUIR UMA FORMA DE PAGAMENTO","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
      }
       
  }

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
